package com.housediyadmin.view
{
	import com.housediyadmin.events.goodsEvents.GetGoodsByTypeIDEvent;
	import com.housediyadmin.events.treeEvents.AddGoodsLevelEvent;
	import com.housediyadmin.events.treeEvents.DeleteGoodsLevelEvent;
	import com.housediyadmin.events.treeEvents.EditGoodsLevelEvent;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.GoodsTreeModel;
	import com.housediyadmin.view.components.BaseButtonCom;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.events.ListEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2010/11/18
	 */	
	public class GoodsTreeAdminMediator extends Mediator
	{
		[Inject]
		public var view:GoodsTreeAdminView;
		
		[Inject]
		public var goodsTypeModel:GoodsTreeModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		public var typeIdArray:Array;
		
		override public function onRegister():void
		{					
			view.addLevelOne.addEventListener(MouseEvent.CLICK, addGoodsLevelOneHandler);
			view.addTypeBtn.addEventListener(MouseEvent.CLICK, addGoodsTypeHandler);
			view.deleteTypeBtn.addEventListener(MouseEvent.CLICK, deleteGoodsLevelHandler);
			view.editTypeBtn.addEventListener(MouseEvent.CLICK, editGoodsTypeHandler);
		}
		
		public function addGoodsLevelOneHandler(event:MouseEvent):void
		{
			var typeName:String = view.addTypeTI.text;
			var typeParentId:int = 0;
			var typeLevel:int = 1;
			dispatch(new AddGoodsLevelEvent(AddGoodsLevelEvent.ADD_GOODS_LEVEL, typeName, typeParentId, typeLevel));
			view.addTypeTI.text = null;
		}
		
		public function addGoodsTypeHandler(event:MouseEvent):void
		{						
			var typeName:String = view.addTypeTI.text;
			var typeParentId:int = (int)(goodsTypeModel.selectedNode.@typeID);
			var typeLevel:int = (int)(goodsTypeModel.selectedNode.@typeLevel)+1;
			if(typeLevel >= 4)
			{
				Alert.show("只能添加商品，不能添加下一级");
				view.addTypeTI.text = null;
			}
			else
			{
				dispatch(new AddGoodsLevelEvent(AddGoodsLevelEvent.ADD_GOODS_LEVEL, typeName, typeParentId, typeLevel));
				view.addTypeTI.text = null;
			}			
		}

		public function deleteGoodsLevelHandler(event:MouseEvent):void
		{
			var typeId:int = (int)(goodsTypeModel.selectedNode.@typeID.toString());
			var typeArr:Array = new Array();
			typeArr.push(typeId);					
			if(goodsTypeModel.selectedNode.children().length() != 0 && goodsTypeModel.selectedNode.@typeLevel != 3)
			{
				Alert.show("该级还存在下级目录，不能删除");
			}
			else if(goodsTypeModel.selectedNode.@typeLevel == 3 )
			{
				dispatch(new GetGoodsByTypeIDEvent(GetGoodsByTypeIDEvent.GET_GOODS_BY_TYPEID, typeArr, null));
				if(goodsModel.goodsArray.length != 0)
				{
					Alert.show("该级下还有商品，不能删除");
				}
				else
				{
					dispatch(new DeleteGoodsLevelEvent(DeleteGoodsLevelEvent.DELETE_GOODS_LEVEL,typeId));
				}
			}
			else{
				dispatch(new DeleteGoodsLevelEvent(DeleteGoodsLevelEvent.DELETE_GOODS_LEVEL,typeId));
			}
		}
		
		public function editGoodsTypeHandler(event:MouseEvent):void
		{
			var typeName:String = view.addTypeTI.text;
			view.addTypeTI.text = null;
			var typeId:int = (int)(goodsTypeModel.selectedNode.@typeID);
            dispatch(new EditGoodsLevelEvent(EditGoodsLevelEvent.EDIT_GOODS_LEVEL, typeId, typeName));
		}
	}
}